import itemModel from 'server/model/itemModel';
import {
    totalConfig,
    propName
} from 'server/config/templateConfig';
import listPackageModel from 'server/model/listPackageModel';
let itemsConfig = totalConfig.item;
const MAX_STACK = 999;

class ListPackageHandle {
    static getInstance() {
        if (!ListPackageHandle.instance) {
            ListPackageHandle.instance = new ListPackageHandle();
        }
        return ListPackageHandle.instance;
    }

    //初始化背包
    initPackag(pack) {
        pack = listPackageModel.create(pack);
        pack.index = 1;
        let len = pack.items.length;
        for (let i = 0; i < len; i++) {
            let itemComponent = pack.items[i];
            if (!itemComponent) {
                continue
            }
            let templateId = itemComponent.templateId;
            itemComponent = itemModel.create(itemComponent);
            itemComponent.index = pack.index;
            pack.index += 1;
            pack.items[i] = itemComponent;
            if (!pack.itemsById[templateId]) {
                pack.itemsById[templateId] = [];
            }
            pack.itemsById[templateId].push(itemComponent);
        }
        return pack;
    }

    //获取背包信息
    getInfo(pack) {
        return {
            items: pack.items,
            size: pack.size,
            emptySize: pack.emptySize
        }
    }

    //获取背包大小
    getSize(pack) {
        return pack.items.length;
    }

    //添加物品
    addItems(pack, items) {
        let size = items.length;
        for (let i = 0; i < size; i++) {
            let item = items[i];
            item.index = pack.index;
            pack.index += 1;
            this.addItemComponent(pack, item);
        }
        return 0;
    }

    //添加物品
    addItem(pack, itemId, num) {
        if (!itemModel.getItemConfig(itemId)) {
            console.log(`${itemId}该物品不存在`);
            return
        }
        let itemComponent = itemModel.create({
            templateId: itemId,
            stack: num,
            index: pack.index
        });
        pack.index += 1;
        return this.addItemComponent(pack, itemComponent);
    }

    //添加物品
    addItemComponent(pack, itemComponent) {
        //判断物品是否可以叠加
        let itemMaxStack = itemModel.getItemConfig(itemComponent.templateId).maxStack || MAX_STACK;
        if (itemMaxStack === 1) {
            //不可叠加，添加到空格子
            this._addItemToPosition(pack, itemComponent);
            return 0;
        }
        //查找物品是否已经存在
        let itemInfoId = pack.itemsById[itemComponent.templateId];
        if (!itemInfoId) {
            //不存在，添加到空格子
            this._addItemToPosition(pack, itemComponent);
        } else {
            let num = itemComponent.stack; //叠加数
            for (let i in itemInfoId) {
                let p = itemInfoId[i]
                let stack = p.stack
                if (stack + num <= MAX_STACK) {
                    p.stack = stack + num;
                    num = 0;
                    return 0;
                } else {
                    num = stack + num - MAX_STACK;
                    p.stack = MAX_STACK;
                }
            }
            if (num > 0) {
                //剩余的数量添加到空格子
                itemComponent.stack = num;
                this._addItemToPosition(pack, itemComponent);
            }
        }
        return 0
    }

    //添加物品到空格子
    _addItemToPosition(pack, itemComponent) {
        pack.items.push(itemComponent);
        let itemInfoId = pack.itemsById[itemComponent.templateId];
        //查找物品是否已经存在
        if (itemInfoId) {
            itemInfoId.push(itemComponent);
        } else {
            pack.itemsById[itemComponent.templateId] = [itemComponent];
        }
    }


    //根据下标删除背包中的物品
    removeItemByPosition(pack, pos) {
        let item = pack.items[pos];
        if (!item) {
            return 1;
        }

        let itemId = item.templateId;
        pack.items.splice(pos, 1);
        let itemInfoById = pack.itemsById[itemId];
        if (itemInfoById.length === 1) {
            delete pack.itemsById[itemId];
        } else {
            let length = itemInfoById.length
            for (let i = 0; i < length; i++) {
                if (itemInfoById[i].index === item.index) {
                    itemInfoById.splice(i, 1);
                    break;
                }
            }
        }
        return 0;
    }

    //清空包裹中的物品
    clearPackage(pack) {
        pack.itemsById = {};
        pack.items = [];
    }

}


export default ListPackageHandle.getInstance();